package com.yootk;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class YootkDemo4 {
    public static int money = 0;
    private static final ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) throws Exception {
        int[] data = new int[] {100, 200, 300};

        for (int x = 0; x < data.length; x++) { // 循环创建子线程
            final int temp = x;
            new Thread(() -> {
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.lock();
                try {
                    money += data[temp];
                } finally {
                    lock.unlock();
                }
            }).start();
        }

        TimeUnit.SECONDS.sleep(1);
        System.out.println("【计算完成】最终存款总额：" + money);
    }
}